package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsGuidePointDetail;
import com.macro.mall.dto.BmsGuidePointParam;
import com.macro.mall.mapper.BmsGuidePointMapper;
import com.macro.mall.mapper.BmsGuidePointPicMapper;
import com.macro.mall.model.BmsGuidePoint;
import com.macro.mall.model.BmsGuidePointPic;
import com.macro.mall.service.BmsGuidePointService;
import com.macro.mall.utils.UserUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 观光车点位图片Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class BmsGuidePointServiceImpl implements BmsGuidePointService {

    @Autowired
    private BmsGuidePointMapper bmsGuidePointMapper;
    @Autowired
    private UserUtils userUtils;
    @Autowired
    private BmsGuidePointPicMapper bmsGuidePointPicMapper;
    @Transactional
    @Override
    public int create(BmsGuidePointParam bmsGuidePointParam) {
        int count = 0;
        Long userId = userUtils.getUserId();
        BmsGuidePoint bmsGuidePoint = new BmsGuidePoint();
        BeanUtils.copyProperties(bmsGuidePointParam, bmsGuidePoint);
        bmsGuidePoint.setCreateUser(userId);
        bmsGuidePoint.setCreateTime(new Date());
        bmsGuidePoint.setUpdateUser(userId);
        bmsGuidePoint.setUpdateTime(new Date());
        if (bmsGuidePointMapper.insert(bmsGuidePoint) > 0){
            //是封面图
            String coverPic = bmsGuidePointParam.getCoverPic();
            //不是封面图
            List<String> picList = bmsGuidePointParam.getPicList();
            BmsGuidePointPic bmsGuidePointPic;
            if (StringUtils.isNotBlank(coverPic)){
                bmsGuidePointPic = new BmsGuidePointPic();
                bmsGuidePointPic.setPic(coverPic);
                bmsGuidePointPic.setGuidePointId(bmsGuidePoint.getId());
                bmsGuidePointPic.setIsCover(1);
                if (bmsGuidePointPicMapper.insert(bmsGuidePointPic) < 0){
                    return -1;
                }
            }
            if (CollectionUtils.isNotEmpty(picList)) {
                for (String pic : picList) {
                    bmsGuidePointPic = new BmsGuidePointPic();
                    bmsGuidePointPic.setPic(pic);
                    bmsGuidePointPic.setGuidePointId(bmsGuidePoint.getId());
                    bmsGuidePointPic.setIsCover(0);
                    if (bmsGuidePointPicMapper.insert(bmsGuidePointPic) <0){
                        return -1;
                    }
                }
            }
            count = 1;
            return count;
        }
        return count;
    }

    /**
     * 获取列表
     * @param bmsGuidePointParam
     * @return
     */
    @Override
    public List<BmsGuidePoint> getList(BmsGuidePointParam bmsGuidePointParam) {
        PageHelper.startPage(bmsGuidePointParam.getPageNum(), bmsGuidePointParam.getPageSize());
        BmsGuidePoint bmsGuidePoint = new BmsGuidePoint();
        BeanUtils.copyProperties(bmsGuidePointParam, bmsGuidePoint);
        List<BmsGuidePoint> list = bmsGuidePointMapper.getList(bmsGuidePoint);
        return list;
    }

    @Override
    public BmsGuidePoint getDetail(Long id) {
        BmsGuidePoint bmsGuidePoint = bmsGuidePointMapper.selectById(id);
        BmsGuidePointDetail detail = new BmsGuidePointDetail();
        BeanUtils.copyProperties(bmsGuidePoint, detail);
        // 获取封面图
        String coverPic = bmsGuidePointMapper.findCoverPicByActivitiesId(id);
        // 获取活动图片
        List<String> picList = bmsGuidePointMapper.findPicListByActivitiesId(id);
        detail.setCoverPic(coverPic);
        detail.setPicList(picList);
        return detail;
    }
    @Transactional
    @Override
    public int delete(List<Long> ids) {
        int count = 0;
        if (bmsGuidePointMapper.delete(ids)>0){
            if (bmsGuidePointPicMapper.delete(ids) > 0){
                count = 1;
                return count;
            }
        }
        return count;
    }

    /**
     * 更新导览点位
     * @param bmsGuidePointParam
     * @return
     */
    @Transactional
    @Override
    public int update(BmsGuidePointParam bmsGuidePointParam) {
        //1.先更新主表的信息，根据id
        int count = 1;
        Long userId = userUtils.getUserId();
        BmsGuidePoint bmsGuidePoint = new BmsGuidePoint();
        BeanUtils.copyProperties(bmsGuidePointParam, bmsGuidePoint);
        bmsGuidePoint.setCreateUser(userId);
        bmsGuidePoint.setCreateTime(new Date());
        bmsGuidePoint.setUpdateUser(userId);
        bmsGuidePoint.setUpdateTime(new Date());
        if (bmsGuidePointMapper.update(bmsGuidePoint) > 0) {
            //如果更新成功，那么就根据主表id关联子表进行删除
            List<Long> ids = new ArrayList<>();
            ids.add(bmsGuidePointParam.getId());
            if (bmsGuidePointPicMapper.delete(ids) > 0){
                BmsGuidePointPic bmsGuidePointPic;
                //是封面图
                String coverPic = bmsGuidePointParam.getCoverPic();
                //不是封面图
                List<String> picList = bmsGuidePointParam.getPicList();
                //新增封面图
                if (StringUtils.isNotBlank(coverPic)){
                    bmsGuidePointPic = new BmsGuidePointPic();
                    bmsGuidePointPic.setPic(coverPic);
                    bmsGuidePointPic.setGuidePointId(bmsGuidePoint.getId());
                    bmsGuidePointPic.setIsCover(1);
                    if (bmsGuidePointPicMapper.insert(bmsGuidePointPic) < 0){
                        return -1;
                    }
                }
                //新增不是封面图
                if (CollectionUtils.isNotEmpty(picList)) {
                    for (String pic : picList) {
                        bmsGuidePointPic = new BmsGuidePointPic();
                        bmsGuidePointPic.setPic(pic);
                        bmsGuidePointPic.setGuidePointId(bmsGuidePoint.getId());
                        bmsGuidePointPic.setIsCover(0);
                        if (bmsGuidePointPicMapper.insert(bmsGuidePointPic) < 0){
                            return -1;
                        }
                    }
                }
            }
        }else {
            count = 0;
            return count;
        }
        return count;
    }

}
